<?php
// +----------------------------------------------------------------------
// | Action: 代理商资料修改控制器
// +----------------------------------------------------------------------
// | Author: chen <vd8888@126.com>
// +----------------------------------------------------------------------
// |   ToDo:
// +----------------------------------------------------------------------
class InformationAction extends CommonAction {
	/*代理商资料修改页面*/
	public function index(){
		$agent_name=$_SESSION["agent"]['username'];
		/*开始获取代理商信息*/
		$agent=D('Proxy');
		$arr_agent=$agent->where("username='".$agent_name."'")->find();

		$this->assign("arr_agent",$arr_agent);
		$this->display();
	}
	public function save(){
		$ajax['status']='error';
		$ajax['Msg']='保存失败！';
		$proxy = D('Proxy');
		$data=$_POST;
		foreach ($data as $k=>$v){
			$v=filter_default($data["$k"]);
			if (empty($v)){
				$ajax['Msg']='提交内容不能为空！';
				echo json_encode($ajax);
				die();
			}elseif ($k == 'username') {
				$ajax['Msg']='你知道的太多了！';
				echo json_encode($ajax);
				die();
			}
			$data["$k"]=trim_str($v);
		}

		$datas['id']=$_SESSION["agent"]['id'];

		$result=$proxy->where($datas)->save($data);

		if ($result){
			$ajax['status']='true';
			$ajax['Msg']='修改成功！';
			echo json_encode($ajax);
		}else {
			$ajax['Msg']='资料未修改！';
			echo json_encode($ajax);
		}
	}
	public function passsave(){
		$ajax['status']='error';
		$ajax['Msg']='密码修改失败！';
		$oldpass=I('post.oldpass');
		$password=I("post.password");

		$datas['id']=$_SESSION["agent"]['id'];
		$datas['password']=md5($oldpass);

		$proxy = D('Proxy');
		$result=$proxy->where($datas)->find();
		$data['password']=md5($password);
		if ($result){
			array_remove($datas,1);
			$result=$proxy->where($datas)->save($data);
			if ($result){
				$ajax['status']='true';
				$ajax['Msg']='密码修改成功！';
				echo json_encode($ajax);
			}else {
				$ajax['Msg']='新密码与原来密码相同！';
				echo json_encode($ajax);
			}

		}else{
			$ajax['Msg']='原来密码不正确！';
			echo json_encode($ajax);
		}

	}
	public function safesave(){
		$ajax['status']='error';
		$ajax['Msg']='安全码修改失败！';
		$oldpass=I('post.oldpass');
		$password=I("post.password");

		$datas['id']=$_SESSION["agent"]['id'];
		$datas['safecode']=md5($oldpass);

		$proxy = D('Proxy');
		$result=$proxy->where($datas)->find();
		$data['safecode']=md5($password);
		if ($result){
			array_remove($datas,1);
			$result=$proxy->where($datas)->save($data);
			if ($result){
				$ajax['status']='true';
				$ajax['Msg']='安全码修改成功！';
				echo json_encode($ajax);
			}else {
				$ajax['Msg']='安全码与原来安全码相同！';
				echo json_encode($ajax);
			}

		}else{
			$ajax['Msg']='原来安全码不正确！';
			echo json_encode($ajax);
		}

	}
}